<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>jsonp</title>
  <script src="index.js"></script>
</head>

<body>
  <button id="jsonp">测试jsonp</button>
  <button id="fetch">测试fetch</button>
  <script>
    // 在全局被调用了
    const callback = (msg) => {
      console.log(msg)
    }

    /* jsonp */
    document.getElementById('jsonp').addEventListener('click', () => {
      window.jsonp({
        url: "http:localhost:8081",
        callback
      })
    });

    /* fetch */
    document.getElementById('fetch').addEventListener('click', async () => {
      const data = await fetch('http:localhost:8081')
        .then(response => response.json());
    });
  </script>
</body>

</html>